﻿// 7-6 树的遍历 (25 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct Bitree {
	char data;
	struct Bitree* lchild;
	struct Bitree* rchild;
}*bitree, bitnode;
bitree Createtree(void);
bitree Restoretree(int h1, int h2, int z1, int z2);
void Preorder(bitree bt);
char in[50], post[50];
int main(void)
{
	int n;
	cin>>n;
	getchar();
	//getchar()用于清除回车
	for (int i = 0; i < n; i++)
		cin>>post[i];
	getchar();
	for (int i = 0; i < n; i++)
		cin>>in[i];
	bitree b = Restoretree(0, n - 1, 0, n - 1);
	Preorder(b);
	printf("\n");
	return 0;
}

bitree Createtree(void)
{
	bitree bt = (bitree)malloc(sizeof(bitnode));
	bt->lchild = bt->rchild = NULL;
	return bt;
}
bitree Restoretree(int h1, int h2, int z1, int z2)
{
	bitree bt = Createtree();
	bt->data = post[h2];
	for (int i = z1; i <= z2; i++)
	{
		if (in[i] == post[h2])
		{
			if (i != z1)//存在左子树,构建左子树
				bt->lchild = Restoretree(h1, h1 + i - z1 - 1, z1, i - 1);
			if (i != z2)//存在右子树,构建右子树
				bt->rchild = Restoretree(h2 - z2 + i, h2 - 1, i + 1, z2);
			break;//找到以后跳出遍历
		}
	}
	return bt;
}
void Preorder(bitree bt)
{
	if (!bt)
		return;
	printf("%c ", bt->data);
	Preorder(bt->lchild);
	Preorder(bt->rchild);
}